﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ValidationPN = ProgNetComponents.Validation.Rules;
using ValidatoinPNDX = ProgNetComponentsDx.Validation.Rules;

namespace Wydatki.Setup.Praca
{
    public partial class FormKontrachenci : MyBaseSqlCeGridForm
    {
        public FormKontrachenci()
        {
            InitializeComponent();
        }
        protected override void FinishEdit()
        {
            base.FinishEdit();
            string val;
            string nipNum;
            foreach (DataRow r in kontrachenciDataSet.STP_KONTRACHENCI.Rows)
            {
                val = r["STP_KON_NIP_NUM"].AsString();
                nipNum=r["STP_KON_NIP"].AsString().XReplace("[^0-9]*","");
                if (nipNum != val)
                    r["STP_KON_NIP_NUM"] = nipNum;
            }
        }
        private List<ValidationPN.ValidationRule> validationRules = new List<ValidationPN.ValidationRule>{
            new ValidationPN.MultiColumnsRequiredValidationRule(new string[] {"STP_KON_KOD", "STP_KON_NAZWA", "STP_KON_NIP", "STP_KON_ADRES"}),
            //new ValidationPN.MultiColumnsUniqueValidationRule("STP_KON_KOD", "STP_KON_NAZWA", "STP_KON_NIP"),
            new ValidatoinPNDX.BrowserUniqueValidationRuleLng("STP_KON_KOD", Init.InitFormBrowserNames.Glowna.KontrachenciKod, "ERR_COL_UNIQUE")
        };

        protected override bool ValidateAll()
        {
            //bool res = kontrachenciDataSet.STP_KONTRACHENCI.ValidateTable(validationRules, null, true, false);
            bool res = base.ValidateAll();
            if (res)
                res = kontrachenciDataSet.STP_KONTRACHENCI.ValidateColUnique("STP_KON_KOD", "Kod musi być unikalny");

            if (res)
                res = kontrachenciDataSet.STP_KONTRACHENCI.ValidateColUnique("STP_KON_NAZWA", "Nazwa musi być unikalna");

            if (res)
                res = kontrachenciDataSet.STP_KONTRACHENCI.ValidateColUnique("STP_KON_NIP", "NIP musi być unikalny");
            
            return res;
        }
    }
}
